function output = eu_tg(pm,r,theta,str,y)

% Computes the expected utility for a TG protocol

% pm is a 5x1 vector of parameters, pm = (alpha,beta,kappa,delta,gamma),
% r is the crra parameter
% theta contains the monetary payoffs theta = (T,R,P,S)
% str contains the pure strategy
% y contains the beliefs y = (y1,y2) 

alpha = pm(1); beta = pm(2); kappa = pm(3); delta = pm(4); gamma = pm(5);

% transform the strategy number to (x1,x2)
allx = ff2n(2);
x = allx(5-str,:);

if r==1 
   pi = log(theta);
else
   pi = (1/(1-r))*(theta.^(1-r)-1);
end

eu = (1-kappa)*(x(1)*(y(2)*pi(2)+(1-y(2))*pi(4))+(1-x(1))*pi(3))...
    +(1-kappa)*(y(1)*(x(2)*pi(2)+(1-x(2))*pi(1))+(1-y(1))*pi(3))...
    +kappa*(x(1)*(x(2)*pi(2)+(1-x(2))*pi(4))+(1-x(1))*pi(3))...
    +kappa*(x(1)*(x(2)*pi(2)+(1-x(2))*pi(1))+(1-x(1))*pi(3))...
    -(alpha*x(1)*(1-y(2))+beta*y(1)*(1-x(2)))*(pi(1)-pi(4))...
    -gamma*y(1)*(1-x(2))*(pi(1)-pi(4));

output = eu;